package com.activenetwork.smlite.testcase.events.createEvents.basics;

import java.util.List;


import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.Test;

import com.activenetwork.smlite.page.event.EventDetailView.EventBasicSubview;
import com.activenetwork.smlite.snippet.EventsSnippet;
import com.activenetwork.smlite.testcase.BaseCase;
import com.activenetwork.util.EnvContext;
import com.activenetwork.util.WaitUtil;
import com.activenetwork.util.verification.gson.FailureAssertion;
import com.activenetwork.util.verification.gson.FailurePoint;

public class Basics011 extends BaseCase {
	
	static final Logger logger = LoggerFactory.getLogger(Basics011.class);
	
	@Test
	public void Verify_added_location_can_be_removed_by_click_Cross_icon(){
		loginBusiness.login();
		
		String searchContent = "smlite";
		EventBasicSubview eventBasicSubview = EventsSnippet.gotoAddEventPage();
		
		// select one address from suggest list
		try{
			eventBasicSubview = eventBasicSubview.selectEventLocationAJAX(searchContent, 1);
		}catch(RuntimeException e){
			EnvContext.failurePointTL.get().add(
					new FailurePoint(Basics011.class.getName(), 
							"Verify select existing location", 
							"Searching string:" + searchContent + " does not provide any suggestion address."));
		}
		
		// Verify the existence of suggested address div
		By suggestAddressBy = By.cssSelector(".autotest-lookup-EventBasicSubview-event-locationref-displaystring div.nexus-lookup div[data-lookup-identifier]");
		List<WebElement> suggestedLocations = webDriver.findElements(suggestAddressBy);
		if(suggestedLocations.size() == 0){
			EnvContext.failurePointTL.get().add(
					new FailurePoint(Basics011.class.getName(), 
							"Verify select existing location", 
							"The suggest address is not added so we need to stop Basics011"));
		}
		
		// Delete the suggested address
		By crossIconBy = By.cssSelector(".autotest-lookup-EventBasicSubview-event-locationref-displaystring div.nexus-lookup div[data-lookup-identifier] a");
		webDriver.findElement(crossIconBy).click();
		WaitUtil waitUtil = new WaitUtil();
		waitUtil.wait(500);
		
		// Check the suggested address has been deleted
		try{
			webDriver.findElement(suggestAddressBy);
			EnvContext.failurePointTL.get().add(new FailurePoint(
					Basics011.class.getName(), 
					"Verify deleting suggested address", 
					"It is unable to delete the suggested address"));
		}catch(NoSuchElementException e){
			logger.info("Successfully delete suggested address");
		}
		
		loginBusiness.logout();
		FailureAssertion.pop(Basics011.class);
		
	}

}
